package com.abigdreamer.im.client.module.main.headtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.JPanel;

import com.abigdreamer.im.client.module.vo.Group;
import com.abigdreamer.im.client.module.vo.MsgFriendGroup;
import com.abigdreamer.im.client.module.vo.MsgUser;

import craky.componentc.JCScrollPane;

public class TreeScrollPane extends JCScrollPane {

	private static final long serialVersionUID = 4122505061158759816L;

	private JPanel panel;
	private Box box;
	private List<MsgFriendGroup> groupList = new ArrayList<MsgFriendGroup>();

	public TreeScrollPane() {
		initTrees();
	}

	public TreeScrollPane(List<MsgFriendGroup> groupList) {
		this.groupList = groupList;
		initTrees();
	}

	public void initTrees() {
		// this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		setBackground(Color.white);
		setAlpha(0.5f);
		setHeaderVisible(false);
		
		panel = new JPanel();
		panel.setBackground(Color.white);
		
		box = Box.createVerticalBox();
		for (Group group : groupList) {
			EIMTree tree = new EIMTree(group);
			box.add(tree);
		}
		panel.setLayout(new BorderLayout());
		panel.add(box, BorderLayout.WEST);
		
		this.setViewportView(panel);
	}

	public void changUserSate(String userId, boolean isOnline) {
		for (MsgFriendGroup group : groupList) {
			for (MsgUser user : group.getUserList()) {
				if (userId.equals(user.getUserID())) {
					user.setOnline(isOnline);
					break;
				}
			}
		}
		box.removeAll();
		for (Group group : groupList) {
			EIMTree tree = new EIMTree(group);
			box.add(tree);
		}
	}
}
